import { Service } from "egg";
import { readFileSync } from "fs";
import { resolve } from "url";
/* interface getVideoListQueries {
  pageSize: number,
  pageNum: number
} */
/**
 * 影视列表
 */
export default class User extends Service {
  /**
   *获取用户电影列表
   **/
  public async getVideoList() {
    const { ctx, app } = this;
    const { Joi } = app;
    let schema = Joi.object({
      pageNum: Joi.number().required(),
      pageSize: Joi.number().max(21).required(),
      type: Joi.number().valid(1, 2).required(),
      status: Joi.string().allow(''),
      nation: Joi.number(),
      region: Joi.number(),
      sort: Joi.number()
    });
    let { error } = ctx.validate(schema, ctx.query);
    if (error) {
      ctx.status = 403;
      return { error: error.message };
    };
    // console.log('app.dbName---', app.dbName);

    let { pageSize, pageNum, type, status, region, sort } = ctx.query;
    if (status) {
      let sql = `SELECT vid,title,total_qisim,coverImg,type,views,sort,region FROM video_list WHERE type = ${type}${region ? ' AND region = ' + region : ''}${sort ? ' AND sort LIKE ' + '\'%' + sort + '%\'' : ''} ORDER BY ${status == 'hot' ? 'views' : 'create_time'} DESC LIMIT ${(Number(pageNum) - 1) * Number(pageSize)},${pageSize}`
      return await app.mysql.get(app.dbName).query(sql)
    } else {
      let sql = `SELECT vid,title,total_qisim,coverImg,type,sort,region FROM video_list WHERE type = ${type}${region ? ' AND region = ' + region : ''}${sort ? ' AND sort LIKE ' + '\'%' + sort + '%\'' : ''} ORDER BY create_time DESC LIMIT ${(Number(pageNum) - 1) * Number(pageSize)},${pageSize}`;
      return await app.mysql.get(app.dbName).query(sql)
    }
  }
  /**
   * 
   *  */
  public async getSingleVideoDetail() {
    const { ctx, app } = this;
    const { Joi } = app;
    let schema = Joi.object({
      vid: Joi.string().required(),
    });
    let { error } = ctx.validate(schema, ctx.query);
    if (error) {
      ctx.status = 403;
      return { error: error.message };
    };
    let res = (await app.mysql.get(app.dbName).select('video_list', {
      where: {
        vid: ctx.query.vid
      }
    })) || []
    return res[0]
  }
  /**
   *获取用户电影列表
   **/
  public async getVideoDetail() {
    const { ctx, app } = this;
    const { Joi } = app;
    let schema = Joi.object({
      vid: Joi.string().required(),
      qisim: Joi.number().required()
    });
    let { error } = ctx.validate(schema, ctx.query);
    if (error) {
      ctx.status = 403;
      return { error: error.message };
    };

    let { vid, qisim } = ctx.query;
    return (await app.mysql.get(app.dbName).query(`SELECT url,url_type FROM series WHERE vid = '${vid}' AND qisim = ${Number(qisim)} AND status = 1`))[0] || {}
  }
  /**
   *获取用户电影列表
   **/
  public async getRandomVideo() {
    const { ctx, app } = this;
    const { Joi } = app;
    let schema = Joi.object({
      size: Joi.number().max(12).required(),
      type: Joi.number().required()
    });
    let { error } = ctx.validate(schema, ctx.query);
    if (error) {
      ctx.status = 403;
      return { error: error.message };
    };
    let { size, type } = ctx.query;
    return await app.mysql.get(app.dbName).query(`SELECT vid,title,total_qisim,coverImg,type,sort,nation FROM video_list WHERE type = ${Number(type)} ORDER BY RAND() LIMIT ${size};`)
  }

  public async getChannelList() {
    return await readFileSync(resolve(__dirname, './video/tv.json'))
  }
  public async analysis() {
    const { app, ctx } = this;
    const { Joi } = app;
    let schema = Joi.object({
      vid: Joi.string().required(),
    });
    let { error } = ctx.validate(schema, ctx.query);
    if (error) {
      ctx.status = 403;
      return { error: error.message };
    };
    app.mysql.get(app.dbName).query(`update video_list set views = views + 1 where vid = '${ctx.query.vid}'`)
    return 'ok'
  }

  public async getVideoByQuery() {
    const { app, ctx } = this;
    const { Joi } = app;
    let schema = Joi.object({
      query: Joi.string().max(30).required(),
    });
    let { error } = ctx.validate(schema, ctx.query);
    if (error) {
      ctx.status = 403;
      return { error: error.message };
    };

    return await app.mysql.get(app.dbName).query(`SELECT vid,title,total_qisim,coverImg,type,sort,nation  FROM video_list WHERE title LIKE '%${ctx.query.query}%' ORDER BY create_time DESC LIMIT 0,12`)
  }

  public async getAppBaseConfig() {

    const { app, ctx } = this;
    const { Joi } = app;
    let schema = Joi.object({
      setting_name: Joi.string().required(),
    });
    let { error } = ctx.validate(schema, ctx.query);
    if (error) {
      ctx.status = 403;
      return { error: error.message };
    };
    let res;
    if (ctx.query.setting_name == 'channelList') {
      res = (await app.mysql.get('Yusup').select('settings', {
        where: { setting_name: ctx.query.setting_name, user: app.dbName }
      })) || [];
    } else {
      res = (await app.mysql.get('Yusup').select('settings', {
        where: { /* setting_name: ctx.query.setting_name, */ user: app.dbName }
      })) || [];
    }

    /* res = res.map(item => {
      return {
        setting_name: item.setting_name,
        setting_content: item.setting_content ? JSON.parse(item.setting_content) : ''
      }
    }); */
    let config: any = {};
    res.forEach(item => {
      if (item.setting_name == 'classifyList' /* || item.setting_name == 'channelList' */) {
      } else {
        config[item.setting_name] = item.setting_content ? JSON.parse(item.setting_content) : '';
      }
    });
    return config
    /* if (res[0]) {
      return JSON.parse(res[0].setting_content)
    } else {
      ctx.status = 500;
    } */
  }
}